using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
namespace AlipayClass
{
	public class AlipayNotify
	{
		private string gateway = "";
		private string _transport = "";
		private string _partner = "";
		private string _key = "";
		private string _input_charset = "";
		private string _sign_type = "";
		private string mysign = "";
		private string responseTxt = "";
		private Dictionary<string, string> sPara = new Dictionary<string, string>();
		private string preSignStr = "";
		public string Mysign
		{
			get
			{
				return this.mysign;
			}
		}
		public string ResponseTxt
		{
			get
			{
				return this.responseTxt;
			}
		}
		public string PreSignStr
		{
			get
			{
				return this.preSignStr;
			}
		}
		public AlipayNotify(SortedDictionary<string, string> inputPara, string notify_id, string partner, string key, string input_charset, string sign_type, string transport)
		{
			this._transport = transport;
			if (this._transport == "https")
			{
				this.gateway = "https://www.alipay.com/cooperate/gateway.do?";
			}
			else
			{
				this.gateway = "http://notify.alipay.com/trade/notify_query.do?";
			}
			this._partner = partner.Trim();
			this._key = key.Trim();
			this._input_charset = input_charset;
			this._sign_type = sign_type.ToUpper();
			this.sPara = AlipayFunction.Para_filter(inputPara);
			this.preSignStr = AlipayFunction.Create_linkstring(this.sPara);
			this.mysign = AlipayFunction.Build_mysign(this.sPara, this._key, this._sign_type, this._input_charset);
			this.responseTxt = this.Verify(notify_id);
		}
		private string Verify(string notify_id)
		{
			string strUrl;
			if (this._transport == "https")
			{
				strUrl = string.Concat(new string[]
				{
					this.gateway,
					"service=notify_verify&partner=",
					this._partner,
					"&notify_id=",
					notify_id
				});
			}
			else
			{
				strUrl = string.Concat(new string[]
				{
					this.gateway,
					"partner=",
					this._partner,
					"&notify_id=",
					notify_id
				});
			}
			return this.Get_Http(strUrl, 120000);
		}
		private string Get_Http(string strUrl, int timeout)
		{
			string result;
			try
			{
				HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(strUrl);
				httpWebRequest.Timeout = timeout;
				HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
				Stream responseStream = httpWebResponse.GetResponseStream();
				StreamReader streamReader = new StreamReader(responseStream, Encoding.Default);
				StringBuilder stringBuilder = new StringBuilder();
				while (-1 != streamReader.Peek())
				{
					stringBuilder.Append(streamReader.ReadLine());
				}
				result = stringBuilder.ToString();
			}
			catch (Exception ex)
			{
				result = "错误：" + ex.Message;
			}
			return result;
		}
	}
}
